/**
 * 
 */
package com.waylau.spring.boot.blog.service;

import com.waylau.spring.boot.blog.domain.Forget;
import com.waylau.spring.boot.blog.domain.Genre;
import com.waylau.spring.boot.blog.domain.HotSearch;
import com.waylau.spring.boot.blog.domain.User;
import com.waylau.spring.boot.blog.repository.ForgetRepository;
import com.waylau.spring.boot.blog.repository.GenreRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;
import java.util.Collection;

/**
 * Gener 服务.
 * 
 * @since 1.0.0 2017年3月30日
 * @author <a href="https://waylau.com">Way Lau</a>
 */
@Service
public class GenreServiceImpl implements GenreService {


	@Autowired
	private GenreRepository genreRepository;

	@Override
	public Genre save(Genre genre) {
		if(genre.getId()!=null){
			String name = genre.getName();
			genre = genreRepository.findOne(genre.getId());
			genre.setName(name);
			genre.setStatus(0);
		}
		return genreRepository.save(genre);
	}

	@Override
	public void delete(Genre genre) {
		genre = genreRepository.findOne(genre.getId());
		genre.setStatus(1);
		genreRepository.save(genre);
	}

	@Override
	public List<Genre> findAvailableGenres() {
		return genreRepository.findByStatus(0);
	}

	@Override
	public Page<Genre> findAvailableGenres(String name,Pageable pageable) {
		name = "%"+name+"%";
		return genreRepository.findByNameLikeAndStatus(name,0,pageable);
	}

	@Override
	public Genre findGenreById(Long id) {
		return genreRepository.findOne(id);
	}

	@Override
	public List<Genre> findRandomGenres(Integer n) {
		return genreRepository.random(n);
	}

	@Override
	public List<Genre> findRandomGenresAndIdNotIn(Collection c, Integer n) {
		return genreRepository.randomAndIdsNotIn(c,n);
	}
}
